package cn.itcast.service.impl;


import cn.itcast.pojo.ReporData;
import cn.itcast.service.IUserService;
import cn.itcast.service.ReportBusinessService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Reference
    private ReportBusinessService reportBusinessService;

    //写操作
    @Override
    public void excelExport(HttpServletResponse response) throws IOException {
        List<Object> list = new ArrayList<>();

        ReporData reporData =  new ReporData();

        List<Map<String, Object>> mapList = reportBusinessService.reportHotSetmeal();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        reporData.setReportDate(simpleDateFormat.format(date));
        reporData.setTodayNewMember(String.valueOf(reportBusinessService.reportTodayNewMember().get("todayNewMember")));
        reporData.setTotalMember(String.valueOf(reportBusinessService.reportTotalMember().get("totalMember")));
        reporData.setThisWeekNewMember(String.valueOf(reportBusinessService.reportThisWeekNewMember().get("thisWeekNewMember")));
        reporData.setThisMonthNewMember(String.valueOf(reportBusinessService.reportThisMonthNewMember().get("thisMonthNewMember")));
        reporData.setTodayOrderNumber(String.valueOf(reportBusinessService.reportTodayOrderNumber().get("todayOrderNumber")));
        reporData.setTodayVisitsNumber(String.valueOf(reportBusinessService.reportTodayVisitsNumber().get("todayVisitsNumber")));
        reporData.setThisWeekOrderNumber(String.valueOf(reportBusinessService.reportThisWeekOrderNumber().get("thisWeekOrderNumber")));
        reporData.setThisWeekVisitsNumber(String.valueOf(reportBusinessService.reportThisWeekVisitsNumber().get("thisWeekVisitsNumber")));
        reporData.setThisMonthOrderNumber(String.valueOf(reportBusinessService.reportThisMonthOrderNumber().get("thisMonthOrderNumber")));
        reporData.setThisMonthVisitsNumber(String.valueOf(reportBusinessService.reportThisMonthVisitsNumber().get("thisMonthVisitsNumber")));

        list.add(reporData);

        for (Map<String, Object> map : mapList) {
            ReporData reporHost = new ReporData();
            reporHost.setName(String.valueOf(map.get("name")));
            reporHost.setYuYueCount(String.valueOf(map.get("setmeal_count")));
            reporHost.setProportion(String.valueOf(map.get("proportion")));

            list.add(reporHost);
        }


        //2.定义导出文件名称
        String fileName = "运营数据统计";

        //3.设置响应对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition","attachment;filename="+new String(fileName.getBytes("gb2312"),"ISO8859-1")+".xls");
        //4.输出流和写入流
        ServletOutputStream out = response.getOutputStream();

        //写流，指定具体执行的class类
        ExcelWriter excelWriter = EasyExcel.write(out, ReporData.class).build();
        //设置sheet
        WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
        //执行写的过程
        excelWriter.write(list, writeSheet);
        //写流完成，会自动关闭流
        excelWriter.finish();
    }
}